﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TI_BAL;

namespace SecuritySystem
{
    public partial class AddEditJournal : Form
    {
        int ACCOUNTS_RECIEVABLE_ID = 4;
        int ACCOUNTS_PAYABLE_ID = 5;

        TIEntities dbContext;
        public AddEditJournal()
        {
            InitializeComponent();
            dbContext = new TIEntities();
            LoadAccounts();
            comboBoxAccountName.SelectedIndex = 0;
            comboBoxAccountName.DropDownStyle = ComboBoxStyle.DropDownList;

            PopulateTransactionCombo();
            comboBoxTransactionType.SelectedIndex = 0;

            Journal journal = (from j in dbContext.Journals.Include("Account") select j).FirstOrDefault();

            string abc = string.Empty;
        }
        private void PopulateTransactionCombo()
        {
            var dict = new Dictionary<string, string>();
            dict.Add("C", "Credit");
            dict.Add("D", "Debit");

            comboBoxTransactionType.DataSource = new BindingSource(dict, null);
            comboBoxTransactionType.DisplayMember = "Value";
            comboBoxTransactionType.ValueMember = "Key";

        }
        private void LoadAccounts()
        {
            List<Account> accounts = (from ac in dbContext.Accounts select ac).ToList();
            comboBoxAccountName.DataSource = null;
            comboBoxAccountName.DataSource = accounts;
            comboBoxAccountName.DisplayMember = "Account_Name";
            comboBoxAccountName.ValueMember = "Account_ID";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int customerOrSupplierId = 0;
            if (this.comboBoxAccountName.SelectedValue.ToString() == ACCOUNTS_RECIEVABLE_ID.ToString() || this.comboBoxAccountName.SelectedValue.ToString() == ACCOUNTS_PAYABLE_ID.ToString())
            {
                customerOrSupplierId = int.Parse(this.comboBoxCustomerOrSupplier.SelectedValue.ToString());
            }

            string r_value=ValidateForm();
            if (r_value == string.Empty)
            {
                Journal journal = new Journal();
                int accountId=Convert.ToInt32(comboBoxAccountName.SelectedValue.ToString());
                Account account = (from acc in dbContext.Accounts where acc.Account_ID == accountId select acc).FirstOrDefault();
                journal.Account = account;
                journal.Journal_Amount = Convert.ToDouble(textBoxAmount.Text);
                journal.Journal_Date = dateTimePickerJournal.Value;
                journal.Journal_Description = textBoxDescription.Text.Trim();
                journal.TransactionType = comboBoxTransactionType.SelectedValue.ToString();
                if (journal.TransactionType == "C")
                {
                    journal.Credit_Amount = journal.Journal_Amount;
                    journal.Debit_Amount = 0;
                }
                else {
                    journal.Debit_Amount = journal.Journal_Amount;
                    journal.Credit_Amount = 0;
                }

                if (this.comboBoxAccountName.SelectedValue.ToString() == ACCOUNTS_RECIEVABLE_ID.ToString()) // Customer - Accounts Recievable
                {
                    journal.Customer_ID = customerOrSupplierId;
                }
                else if (this.comboBoxAccountName.SelectedValue.ToString() == ACCOUNTS_PAYABLE_ID.ToString()) // Suppier - Accounts Payable
                {
                    journal.Supplier_ID = customerOrSupplierId;
                }

                dbContext.AddToJournals(journal);
                dbContext.SaveChanges();

                MessageBox.Show("Journal saved sucessfully.");
            }
            else
            {
                MessageBox.Show(r_value);
            }
        }
        private string ValidateForm()
        {
            if(textBoxAmount.Text.Trim().Length == 0)
            {
                return "Please enter some valid value for amount";
            }
           
            return "";
        }

        private void AddEditJournal_Load(object sender, EventArgs e)
        {
            //Account_Name
        }

        private void comboBoxAccountName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxAccountName.SelectedValue.ToString() == ACCOUNTS_RECIEVABLE_ID.ToString()) // Customer - Accounts Recievable
            {
                this.comboBoxCustomerOrSupplier.Visible = true;
                FillCustomerCombo(); 
            }
            else if (this.comboBoxAccountName.SelectedValue.ToString() == ACCOUNTS_PAYABLE_ID.ToString()) // Suppier - Accounts Payable
            {
                this.comboBoxCustomerOrSupplier.Visible = true;
                FillSupplierCombo();
            }
            else {
                this.comboBoxCustomerOrSupplier.Visible = false;
            }
        }

        private void FillSupplierCombo()
        {

            List<Supplier> SupplierList = (from pt in dbContext.Suppliers select pt).ToList<Supplier>();
            if (SupplierList != null)
            {
                BindDataWithSupplierCombo(SupplierList);
            }
            else
            {
                SupplierList = new List<Supplier>();
            }

        }

        private void BindDataWithSupplierCombo(List<Supplier> SupplierList)
        {

            this.comboBoxCustomerOrSupplier.DataSource = null;
            comboBoxCustomerOrSupplier.DisplayMember = "CompanyName";
            comboBoxCustomerOrSupplier.ValueMember = "ID";
            comboBoxCustomerOrSupplier.DataSource = SupplierList;
            comboBoxCustomerOrSupplier.Refresh();
        }

        private void FillCustomerCombo()
        {

            List<Customer> CustomerList = (from pt in dbContext.Customers select pt).ToList<Customer>();
            if (CustomerList != null)
            {
                BindDataWithCustomerCombo(CustomerList);
            }
            else
            {
                CustomerList = new List<Customer>();
            }

        }

        private void BindDataWithCustomerCombo(List<Customer> CustomerList)
        {

            this.comboBoxCustomerOrSupplier.DataSource = null;
            comboBoxCustomerOrSupplier.DisplayMember = "CustomerCompanyName";
            comboBoxCustomerOrSupplier.ValueMember = "ID";
            comboBoxCustomerOrSupplier.DataSource = CustomerList;
            comboBoxCustomerOrSupplier.Refresh();
        }
    }
}
